#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int size;
int *ar;
int flag=0;
void display();
void append();
void extend();
void insert();
void remove1();
void pop();
void index1();
void count();
void sort();
void reverse();

void main()
{
system("clear");
printf("How many elements do you want to enter in the initial main list ? \n");
scanf("%d", &size);
printf("Enter the elements of the list : \n");
ar=calloc(1, sizeof(int));
int ele;
int counter;
for(counter=0;counter<size;counter++)
	{printf("Enter element %d : ", counter+1);
	scanf("%d", &ele);
	ar = realloc(ar, (counter+1)*sizeof(int));
	ar[counter]=ele;
	flag=1;
	}

char ch='y';
while(ch=='y')
{system("clear");
display();
printf("\nYou can perform following functions on the list : \n1. Append\n2. Extend\n3. Insert\n4. Remove\n5. Pop\n6. Index\n7. Count\n8. Sort\n9. Reverse\n10. Display\n\nEnter the serial no. of function you want to perform or press 0 to exit : ");
int opt;
scanf("%d", &opt);
switch(opt)
	{case 0: exit(0);
	case 1 :append();
		break;
	case 2: extend();
		break;
	case 3: insert();
		break;
	case 4: remove1();
		break;
	case 5: pop();
		break;
	case 6: index1();
		break;
	case 7: count();
		break;
	case 8: sort();
		break;
	case 9: reverse();
		break;
	case 10: display();
		break;
	default: printf("Invalid Input!\n");
	}
fflush(stdin);
printf("Do you want to perform more operations? Enter 'y' for yes or any other character to exit.\n");
scanf("%s", &ch);
if(ch!='y')
printf("Thanks for using the program. \n");
}
}
void display()
{
int j;
printf("The current list is : \n[");
if(flag==0)
printf("]\n");
else
	{
	for(j=0; j<size-1; j++)
	{printf(" %d ,", ar[j]);}
	printf(" %d ]\n", ar[j]);
	}
}
void append()
{ printf("Enter the element you want to append : ");
int num;
scanf("%d", &num);
ar = realloc(ar, (size)*sizeof(int));
		ar[size++]=num;
display();
}

void extend()
{printf("Enter the no of elements in second list to extend the main list : ");
int size2;
scanf("%d", &size2);
printf("Enter the elements of second list : \n");
int *ar2;
ar2=calloc(1, sizeof(int));
int ele;
int flag2=0;
int counter;
for(counter=0;counter<size2;counter++)
	{printf("Enter element %d : ", counter+1);
	scanf("%d", &ele);
	ar2 = realloc(ar2, (counter+1)*sizeof(int));
	ar2[counter]=ele;
	flag2=1;
	}

int j;
printf("The list you have entered is : \n[");
if(flag2==0)
printf("]\n");
else
	{
	for(j=0; j<size2-1; j++)
	printf(" %d ,", ar[j]);
	printf(" %d ]\n", ar[j]);
	}
int k;
for(k=0; k<size2; k++)
	{ar = realloc(ar,(size+1)*sizeof(int));
		ar[size++]=ar2[k];
	}
display();
}

void insert()
{printf("Enter the element you want to insert : ");
int num;
scanf("%d", &num);
printf("Enter the position where you want to insert an element : ");
int pos;
scanf("%d", &pos);
if(pos<1 || pos>size+1)
printf("Invalid position!\n");
else
{ar = realloc(ar, (size++)*sizeof(int));
int z;
for(z=size-1; z>=pos; z--)
ar[z]=ar[z-1];		
ar[z]=num;
}
display();
}

void remove1()
{int flag3=0;
printf("Enter the element you want to remove : ");
int num;
scanf("%d", &num);
int k;
for(k=0; k<size; k++)
	{if(ar[k]==num)
		{int z;
		for(z=k; z<size; z++)
		ar[z]=ar[z+1];
		ar = realloc(ar, (--size)*sizeof(int));
		flag3=1;
		break;
		}
	
	}
if(flag3==0)
printf("No such element! \n");
display();
}
void pop()
{ printf("Enter the index no. of element you want to remove : ");
int pos;
scanf("%d", &pos);
if(pos<1 || pos>size+1)
printf("Invalid position!\n");
else
	{int z;
	printf("The element which has been removed is %d.\n", ar[pos-1]);
	for(z=pos-1; z<size-1; z++)
		ar[z]=ar[z+1];
	ar = realloc(ar, (--size)*sizeof(int));
	}

display();
}
void index1()
{
printf("Enter the element whose index of first occurence you want : \n");
int num;
scanf("%d", &num);
int k;
int pos;
int flag4=0;
for(k=0; k<size; k++)
	{if(ar[k]==num)
	{pos=k;
	flag4=1;
	break;
	}
	}
if (flag4==0)
printf("This element is not there in the list.\n");
else
printf("First occurence of this elements is at position : %d\n", pos+1);
}
void count()
{
printf("Enter the element whose count you want : ");
int num;
scanf("%d", &num);
int k;
int count=0;
for(k=0; k<size; k++)
	{if(ar[k]==num)
		count++;
	}
printf("No. of occurences of %d is %d.\n", num, count);
}
void sort()
{
int i,j, temp;
for(i=0; i<size-1; i++)
	{
	for(j=0; j<size-1; j++)
		{
		if(ar[j]>ar[j+1])
			{temp=ar[j];
			ar[j]=ar[j+1];
			ar[j+1]=temp;
			}
		}
	}
printf("List has now been sorted : \n");
display();
}
void reverse()
{
int k;
int temp;
for(k=0; k<size/2; k++)
{
temp=ar[k];
ar[k]=ar[size-1-k];
ar[size-1-k]=temp;
}
printf("The list has been reversed : \n");
display();
}
